package com.bobo.rbac.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bobo.rbac.auth.utils.ResponseResult;
import com.bobo.rbac.domain.User;
import com.bobo.rbac.service.LoginService;
import com.zhenzi.sms.ZhenziSmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: BOBO
 * @time: 2022/7/23 11:36
 */
@Slf4j
@RequestMapping("auth")
@RestController
public class LoginController {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    LoginService loginService;

    /**
     * 产生验证码 输出
     * @param response
     * @param uuid
     * @throws IOException
     */
    @RequestMapping("getImgCode")
    public void getImgCode(HttpServletResponse response, String uuid) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
        captcha.write(response.getOutputStream());//1。输出到浏览器
        log.info("code---：" + captcha.getCode());
        redisTemplate.opsForValue().set(uuid, captcha.getCode(), 15, TimeUnit.MINUTES);//2 存入redis

    }

    @RequestMapping("login")
    public ResponseResult login(@RequestBody User user) {//@RequestBody json-->java


        //从redis获取存储程验证码
    /*    Object redisCode = redisTemplate.opsForValue().get(user.getUuid());
        if (null == redisCode)
            return ResponseResult.fail(300, "验证码失效");
        if (!redisCode.equals(user.getImgcode())) {

            return ResponseResult.fail(300, "验证码错误");
        }*/

        return loginService.login(user);
    }


    @RequestMapping("phonelogin")
    public ResponseResult phonelogin(@RequestBody User user) {//@RequestBody json-->java

        //从redis获取存储程验证码
        Object redisCode = redisTemplate.opsForValue().get(user.getPhone());
        if (null == redisCode)
            return ResponseResult.fail(300, "验证码失效");
        if (!redisCode.equals(user.getImgcode())) {

            return ResponseResult.fail(300, "验证码错误");
        }
        return loginService.loginByPhone(user);
    }

//获取手机验证码
    @RequestMapping("getPhoneMessage")
    public JSONObject getPhoneMessage(String phone) throws Exception {

          String apiUrl ="https://sms_developer.zhenzikj.com";
          String appId="110630";
          String appSecret="ZmI1Yjc0ZTUtODE4Ny00ZjIwLTg2YjQtNDI0MDFjZDljMTZl";

       ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", phone);
        params.put("templateId", 7739);
        String[] templateParams = new String[2];
        RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
        String code = randomGenerator.generate();//生成4位码
        templateParams[0] = code;
        templateParams[1] = "30分钟";
        params.put("templateParams", templateParams);

        String result = client.send(params);//发送
        JSONObject jsonObject = JSON.parseObject(result);
        redisTemplate.opsForValue().set(phone,code,30,TimeUnit.MINUTES);//redis存储一份
         return jsonObject;
       }


}
